package lucene.utils;

import java.io.*;

/**
 * Created by User on 2017/11/29.
 */
public class FileUtil {

    public static void eachFileDir(File file, EachFileDir eachFileDir) throws Exception {
        if (!file.exists()) throw new RuntimeException("file is not exists");;

        if (file.isFile()) {
            eachFileDir.processFile(file);
        } else if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                eachFileDir(f, eachFileDir);
            }
        } else {
            throw new RuntimeException("file neither is file nor is directory");
        }
    }

    public static String readFileToString(File file) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        StringBuilder stringBuilder = new StringBuilder();
        String stringLine;
        while ((stringLine = bufferedReader.readLine()) != null) {
            stringBuilder.append(stringLine).append("\r\n");
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\User\\IdeaProjects\\tm\\app\\views\\Application\\award.html");
        System.out.println(readFileToString(file));
    }




}
